package imserver.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import imserver.beans.MemberInfo;
import imserver.common.util.BaseResponse;
import imserver.common.util.DaoException;
import imserver.common.util.StringUtil;
import imserver.constant.LoginStatus;
import imserver.service.MemberService;

/**
 * 会员管理相关
 * @author zhanghongbin01
 *
 */

@Controller
@RequestMapping("/member")
public class MemberController {
	
	private Logger log = Logger.getLogger(MemberController.class);
	@Resource
	private MemberService memberService;
	
	
	/**
	 * 用户登录
	 * @return
	 * @throws DaoException 
	 */
	@RequestMapping("/login")
	@ResponseBody
	public BaseResponse login(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		response.setStatus(200);
		String num = request.getParameter("num");
		String pass = request.getParameter("pass");
		if(StringUtil.isNullOrEmpty(num) || StringUtil.isNullOrEmpty(pass)){
			response.setErrorno(-1);
			response.setMsg("账号或者密码不可为空");
		}
		int result = memberService.login(num, pass, request);
		response.setErrorno(result);
		switch (result) {
		case LoginStatus.LOGIN_NOT_EXISTS:
			response.setMsg("账号不存在，请先注册吧");
			break;
		case LoginStatus.LOGIN_PASS_ERROR:
			response.setMsg("密码错误，再想想吧！");
			break;
		default:
			response.setMsg("哦，遇到不知名错误了，再试试吧！");
			break;
		}
		return response;
	}
	
	/**
	 * 用户注册
	 * @return
	 * @throws DaoException 
	 */
	@RequestMapping("/register")
	@ResponseBody
	public BaseResponse register(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		String nicheng = request.getParameter("nicheng");
		String pass = request.getParameter("pass");
		if(StringUtil.isNullOrEmpty(nicheng) || StringUtil.isNullOrEmpty(pass)){
			response.setErrorno(200);
			response.setStatus(-1);
			log.warn("Register member exception: The 'nicheng' and 'pass' can not be null!");
			response.setMsg("Register member exception: The 'nicheng' and 'pass' can not be null!");
		}else{
			MemberInfo member = new MemberInfo();
			member.setNicheng(nicheng);
			member.setPass(pass);
			memberService.register(member);
			response.setErrorno(200);
			response.setStatus(0);
			response.setMsg("ok");
		}
		return response;
	}
	
	
	/**
	 * 通过账户搜索一个好友
	 * @return
	 * @throws DaoException 
	 */
	@ResponseBody
	public MemberInfo search(HttpServletRequest request) throws DaoException{
		MemberInfo memberInfo = null;
		String memberNum = request.getParameter("memberNum");
		if(!StringUtil.isNullOrEmpty(memberNum)){
			memberInfo = memberService.getMember(memberNum);
		}
		return memberInfo;
	}
	
	/**
	 * 获取信息
	 * @return
	 */
	public MemberInfo get(){
		return null;
	}
}
